const state = {
  videoList: [],
  indexList: [],
  videoFlag: false,
  videoObj: {}
}

const mutations = {
  //    获取所有的视频id
  GET_VIDEO: (state, payload) => {
    state.videoList.push(payload)
    state.indexList.push(payload.parentIndex)
    state.indexList.sort(function (a, b) {
      return a - b
    })
    state.videoObj = state.videoList.find((item) => {
      return item.parentIndex == state.indexList[0]
    })
    // console.log(state.videoObj, 'getvideo')
  },
  DELETE_VIDEO: (state, payload) => {
    // 计算数字
    // 获取video所在的索引

    let index = state.videoList.findIndex((item) => {
      return item.refId == payload.refId
    })
    let delIndex = state.indexList.findIndex((item) => {
      return item == payload.parentIndex
    })
    // 删除已消失的元素
    delIndex < 0 ? '' : state.indexList.splice(delIndex, 1)
    index < 0 ? '' : state.videoList.splice(index, 1)
    state.indexList.sort(function (a, b) {
      return a - b
    })
    if (state.indexList[0] === 0) {
      state.videoFlag = true
    }
  }
}

const actions = {}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
